package master.ghrm.domain;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import javax.persistence.Version;

@Entity
@Table(uniqueConstraints = @UniqueConstraint(columnNames = { "name" }))
public class RoleType extends AbstractBaseEntity {

	private static final long serialVersionUID = 1L;

	public static final int NAME_LENGTH = 45;
	public static final int DESCRIPTION_LENGTH = 1000;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "role_type_id", unique = true, nullable = false)
	private Integer roleTypeId;

	@Column(length = NAME_LENGTH, nullable = false)
	private String name;

	@Column(length = DESCRIPTION_LENGTH)
	private String description;

	@Version
	private Long version;

	@ManyToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, fetch = FetchType.LAZY)
	@JoinTable(name = "tbl_profile_role_type", joinColumns = { @JoinColumn(name = "role_type_id", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "profile_id", nullable = false, updatable = false) })
	private List<Profile> profiles;

	@Column(name = "order_number", nullable = false)
	private Integer orderNumber;

	/**
	 * Default constructor, for persistence use only.
	 */
	public RoleType() {
		super();
		roleTypeId = null;
		profiles = null;
	}

	/**
	 * Minimal constructor.
	 * 
	 * @param aRoleTypeId ID.
	 */
	public RoleType(Integer aRoleTypeId) {
		this();
		this.roleTypeId = aRoleTypeId;
	}

	/**
	 * @return ID.
	 */
	public Integer getRoleTypeId() {
		return roleTypeId;
	}

	/**
	 * For persistence use only.
	 * 
	 * @param aRoleTypeId ID.
	 */
	public void setRoleTypeId(Integer aRoleTypeId) {
		this.roleTypeId = aRoleTypeId;
	}

	/**
	 * @return name.
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param aName name
	 */
	public void setName(String aName) {
		this.name = aName;
	}

	/**
	 * @return description.
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * @param aDescription description
	 */
	public void setDescription(String aDescription) {
		this.description = aDescription;
	}

	/**
	 * For persistence use only.
	 * 
	 * @return entity version
	 */
	public Long getVersion() {
		return version;
	}

	/**
	 * For persistence use only.
	 * 
	 * @param aVersion entity version
	 */
	public void setVersion(Long aVersion) {
		this.version = aVersion;
	}

	/**
	 * @return user profiles of this role type.
	 */
	public List<Profile> getProfiles() {
		return profiles;
	}

	/**
	 * @param aProfiles users of this role type.
	 */
	public void setProfiles(List<Profile> aProfiles) {
		this.profiles = aProfiles;
	}

	/**
	 * @see com.levi9.hrm.model.base.AbstractBaseEntity#getExcludedAttributes()
	 */
	@Override
	protected String[] getExcludedAttributes() {
		return new String[] { "profiles" };
	}

	/**
	 * @return the orderNumber
	 */
	public Integer getOrderNumber() {
		return orderNumber;
	}

	/**
	 * @param anOrderNumber the orderNumber to set
	 */
	public void setOrderNumber(Integer anOrderNumber) {
		this.orderNumber = anOrderNumber;
	}

	/**
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + ((roleTypeId == null) ? 0 : roleTypeId.hashCode());
		return result;
	}

	/**
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (!super.equals(obj)) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		RoleType other = (RoleType) obj;
		if (name == null) {
			if (other.name != null) {
				return false;
			}
		} else if (!name.equals(other.name)) {
			return false;
		}
		if (roleTypeId == null) {
			if (other.roleTypeId != null) {
				return false;
			}
		} else if (!roleTypeId.equals(other.roleTypeId)) {
			return false;
		}
		return true;
	}

}
